package cn.ghb.demo.common.service;

import cn.ghb.demo.common.dao.CrudMapper;
import cn.ghb.demo.common.pojo.CrudEntity;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.time.LocalDateTime;
import java.time.ZoneOffset;

/**
 * @author HongbinGao
 */
public class CrudService<M extends CrudMapper<T>, T extends CrudEntity> extends ServiceImpl<M, T> {

    protected void insertOrUpdate(T entity) {
        Integer userId = 1;
        LocalDateTime now = LocalDateTime.now();
        if (entity.getId() == null) {
            entity.setInsertBy(userId);
            entity.setInsertTime(now);
        } else {
            entity.setUpdateBy(userId);
            entity.setUpdateTime(now);
        }
    }

    @Override
    public boolean save(T entity) {
        this.insertOrUpdate(entity);
        return super.save(entity);
    }

    @Override
    public boolean saveOrUpdate(T entity) {
        this.insertOrUpdate(entity);
        return super.saveOrUpdate(entity);
    }

}
